/////////////////////
// RPE-LTP encoder //
/////////////////////

#include "fxp.h"
#include "defines.h"
#include "lar_logic.h"
#include "rpe_logic.h"

#ifndef RPE_LTP_TX_H
#define RPE_LTP_TX_H

// Downscaling of the input signal(5.2.1)
void input_downscale(int16_t* input);

// Offset compensation(5.2.2)
// sof[k]=so[k]-so[k-1]+alpha*sof[k-1]
// alpha=32735*2^-5
void offset_compensation(int16_t* so,int16_t* sof,int16_t* z1,int32_t* L_z2);

// Pre-emphasis(5.2.3)
void pre_emphasis(int16_t* sof,int16_t* s,int16_t* mp);

// Autocorrelation(5.2.4)
void autocorrelation(int16_t* s,int32_t* L_ACF);

// Computation of the reflection coefficients(5.2.5)
void reflection_coeffs(int32_t *L_ACF,int16_t *r);

// Transform the reflection coefficients to Log-Area Ratios(5.2.6)
// The following scaling has been used
/* r[..] = integer( real_r[..]*32768. ); -1. <= real_r <1. */
/* */
/* LAR[..] = integer( real_LAR[..]*16384. ); */
/* */
/* with -1.625 <= real_LAR <= 1.625 */
void refl_to_lar(int16_t* r, int16_t* LAR);

// Quantization and coding of the Log-Area Ratios(5.2.7)
void lar_to_larc(int16_t* LAR,int16_t* LARc);

// Calculation of the LTP parameters(5.2.11)
void ltp_params(int16_t* d,int16_t* dp,int* bc,int* Nc,int k);

// Long term analysis filtering (5.2.12)
void lt_filter(int bc, int Nc, int16_t* d,int16_t* dp, int16_t* dpp,int16_t* e,int k);

// Weighting filter (5.2.13)
void w_filter(int16_t*e ,int16_t* x,int k);

// RPE grid selection(5.2.14)
void grid_selection(int16_t* x, int* Mc,int k);
void down_sampling_by3(int16_t*x,int Mc,int16_t*xM,int k);

// APCM quantization of the selected RPE sequence(5.2.15)
void apcm_q_of_rpe_seq(int16_t*xM, int16_t* xmaxc, int* mant, int *exp,int16_t* xMc,int k);

// Update of the reconstructed short term residual signal [-120..-1](5.2.18)
void update_reconstructed_st_residual_signal(int16_t* ep,int16_t* dpp,int16_t* dp,int k);

// Encoder prototype
void rpeltp_encoder(
		// Input(primary frame)
		int16_t* p_frame,
		// Output
		// For each primary frame
		int16_t* LARc,
		// For each secondary frame
		int16_t* LTP_P1,
		int16_t* LTP_b1,
		int16_t* RPE_M1,
		int16_t* RPE_xmaxc1,
		int16_t* RPE_xMc1,

		int16_t* LTP_P2,
		int16_t* LTP_b2,
		int16_t* RPE_M2,
		int16_t* RPE_xmaxc2,
		int16_t* RPE_xMc2,

		int16_t* LTP_P3,
		int16_t* LTP_b3,
		int16_t* RPE_M3,
		int16_t* RPE_xmaxc3,
		int16_t* RPE_xMc3,

		int16_t* LTP_P4,
		int16_t* LTP_b4,
		int16_t* RPE_M4,
		int16_t* RPE_xmaxc4,
		int16_t* RPE_xMc4
);

void pack_decoder_homing_frame(
				// Input
				// For each primary frame
				int16_t* LARc,
				// For each secondary frame
				int16_t* LTP_P1,
				int16_t* LTP_b1,
				int16_t* RPE_M1,
				int16_t* RPE_xmaxc1,
				int16_t* RPE_xMc1,

				int16_t* LTP_P2,
				int16_t* LTP_b2,
				int16_t* RPE_M2,
				int16_t* RPE_xmaxc2,
				int16_t* RPE_xMc2,

				int16_t* LTP_P3,
				int16_t* LTP_b3,
				int16_t* RPE_M3,
				int16_t* RPE_xmaxc3,
				int16_t* RPE_xMc3,

				int16_t* LTP_P4,
				int16_t* LTP_b4,
				int16_t* RPE_M4,
				int16_t* RPE_xmaxc4,
				int16_t* RPE_xMc4,
				// Output - Packed voice frame
				uint8_t* dhf
);
#endif
